<?php 

namespace FCJ\EuroDisney;


require_once 'php_Util_lib.php';
use php_Util as ns_php_Util;

require_once 'FCJ_DB/FCJ_DB.php';
require_once 'FCJ_DB/FCJ_DB_Users.php';
require_once 'FCJ_DB/FCJ_DB_EuroDisney_Form.php';
require_once 'FCJ_DB/FCJ_DB_Groups.php';
require_once 'FCJ_DB/FCJ_DB_Members.php';
require_once 'FCJ_DB/FCJ_DB_Members_Details.php';
require_once 'FCJ_DB/FCJ_DB_TripToEuroDisney_2013.php';

// require_once 'JSON.php';	// Required for servers with PHP 4.3 o older

use FCJ\Database as ns_FCJ_DB;



/**
 * EuroDisney trip form CONSTANTS
 * Fundació Juvanteny 
 * @author Felipe
 *
 */
class FCJ_EuroDisney_CONSTANTS {
	const FUNCTION__Add_Family = "addFamily";
	const FUNCTION__Load_Ref_Users = "loadRefUsers";
	
	const EXTRA_DATA__Other_Transport_Detail = "Other_Transport_Detail";
	
}


/**
 * EuroDisney trip form
 * Fundació Juvanteny 
 * 
 * 
 * @author Felipe
 * @since 2013/02/19
 *
 */
class FCJ_EuroDisney {
	
	/**
	 * Manage ajax functions
	 */
	public function manage_AJAX() {

		
		// Check SSL ---------------------------------------------------------|\/|---
		if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off') {
			// SSL connection
			throw new \Exception("Secure connection required.");
		}
		// -------------------------------------------------------------------|/\|---		
		
		
		$function = $_GET['function'];
		
		
		switch ($function) {
			
			case FCJ_EuroDisney_CONSTANTS::FUNCTION__Add_Family :
				return $this->add_Family_DATA();
				break;
				
			case FCJ_EuroDisney_CONSTANTS::FUNCTION__Load_Ref_Users :
				return $this->get_ReferentLinks();
				break;
			
		}
		
		
	}
	
	
	
	/**
	 * Return reference links
	 * @throws \Exception
	 * @return string
	 */
	protected function get_ReferentLinks() {
		
// 		$search_Key = $_POST['search_Key'];
		
		$user_DATA = ns_FCJ_DB\FCJ_DB_Users_FACTORY::get_NEW__FCJ_DB_User_DATA();
		$users_EDITOR = ns_FCJ_DB\FCJ_DB_Users_FACTORY::get_NEW__FCJ_DB_Users_EDITOR();
		$users_TABLE = $users_EDITOR->get_Description_Table();
		
		
		
		$fields = array(
				$users_TABLE->field_id,
				$users_TABLE->field_user_Name
				);
		
		$consitions = array(
				$users_TABLE->field_user_type => ns_FCJ_DB\FCJ_DB_Users_CONSTANTS::USER_TYPE__EDUCATOR
		);
		

		$dbo_Editor =  ns_php_Util\DB\php_Util_DB_FACTORY::get_NEW__php_Util_DB_Editor(ns_php_Util\DB\php_Util_DB_CONSTANTS::DB_CONNECTION_TYPE__PDO);
		
		try {
			$dbo_Editor->connect_To_DB( ns_FCJ_DB\FCJ_DB_FACTORY::get_Connection() );	
		} catch (\Exception $e) {
			throw new \Exception("Error in DB connection. ". $e->getMessage());
		}
		
		
		$result_Array = null;
		
		try {
			$result_Array = $dbo_Editor->read_Object($users_TABLE->tableName, $consitions, $fields);
		} catch (\Exception $e) {
			throw new \Exception("Error reading objects. ". $e->getMessage());
		}
		
		
		$result = array(
				"referent_Users" => $result_Array
				);
		
		$result_JSON = json_encode($result, 256);

		
		return $result_JSON;
		
	}
	
	
	/**
	 * Process EuroDisney Form data
	 * @param ns_FCJ_DB\FCJ_DB_EuroDisney_Form_DATA $fcj_DB_EuroDisney_Form__DATA
	 */
	protected function process_EuroDisney_Form_DATA($fcj_DB_EuroDisney_Form__DATA, $connection) {
		
		// Initialize required objects --------------------------------------------------------------------|\/|---
		$fcj_Groups_EDITOR = ns_FCJ_DB\FCJ_DB_Groups_FACTORY::get_NEW__FCJ_DB_Groups_EDITOR();
		$fcj_Groups_TABLE = $fcj_Groups_EDITOR->get_Description_Table();
		// ------------------------------------------------------------------------------------------------|/\|---
		
		
		// Check required parameters ----------------------------------------------------------------------------------------------------------------|\/|---
		if ( ($fcj_DB_EuroDisney_Form__DATA->group_name == null || $fcj_DB_EuroDisney_Form__DATA->group_name == "") || 
			($fcj_DB_EuroDisney_Form__DATA->group_email == null || $fcj_DB_EuroDisney_Form__DATA->group_email == "") || 
			($fcj_DB_EuroDisney_Form__DATA->group_phone == null || $fcj_DB_EuroDisney_Form__DATA->group_phone == "") || 
			($fcj_DB_EuroDisney_Form__DATA->group_user_Link_ID == null || $fcj_DB_EuroDisney_Form__DATA->group_user_Link_ID == "") ||
			($fcj_DB_EuroDisney_Form__DATA->group_Payment_Method == null || $fcj_DB_EuroDisney_Form__DATA->group_Payment_Method == "") || 
			($fcj_DB_EuroDisney_Form__DATA->group_Transport_Method == null || $fcj_DB_EuroDisney_Form__DATA->group_Transport_Method == "")) {
			
			throw new \Exception("Check required parameters. "
					. print_r($fcj_DB_EuroDisney_Form__DATA, true), 
					ns_FCJ_DB\FCJ_DB_EuroDisney_Form_CONSTANTS::SUBMIT_RESULT__ERROR__BAD_PARAMETERS);
			
		}
		// ------------------------------------------------------------------------------------------------------------------------------------------|/\|---		
		
		
		// Check members data -----------------------------------------------------------------------------------------------------------------|\ Check Members data /|---
		$array_Members_JSON = json_decode($fcj_DB_EuroDisney_Form__DATA->members_DATA);
		if (!is_array($array_Members_JSON) || (sizeof($array_Members_JSON) < 1)) {
			throw new \Exception("Member data is required.", ns_FCJ_DB\FCJ_DB_EuroDisney_Form_CONSTANTS::SUBMIT_RESULT__ERROR__MEMBER_DATA);
		}
		
		
		$array_Members_Info = array();
		
		for ($index_member = 0; $index_member < sizeof($array_Members_JSON); $index_member++) {
			
			$member_DATA_JSON = $array_Members_JSON[$index_member];
			
			// If member is deleted... skip ----------------------|\/|---
			if ($array_Members_JSON->{'_deleted'} != null) {
				continue;
			}
			// ---------------------------------------------------|/\|---
			
			// Set member data ------------------------------------------------------------------------|\/|---
			$fcj_Member_DATA = ns_FCJ_DB\FCJ_DB_Members_FACTORY::get_NEW__FCJ_DB_Member_DATA();
			$fcj_Member_DATA->name = $member_DATA_JSON->{'name'};
			$fcj_Member_DATA->surname = $member_DATA_JSON->{'surname'};
			$fcj_Member_DATA->member_Doc_ID = $member_DATA_JSON->{'dni'};
			// ----------------------------------------------------------------------------------------|/\|---
			
			$fcj_Member_Datails_DATA = ns_FCJ_DB\FCJ_DB_Members_Details_FACTORY::get_NEW__FCJ_DB_Member_Details_DATA();
			
			$date_of_birth_DATE_Object = \DateTime::createFromFormat('d-m-y', str_replace('/', '-', $member_DATA_JSON->{'dateofbirth'}) );
			$fcj_Member_Datails_DATA->date_of_birth = date("Y-m-d H:i:s", strtotime( str_replace('/', '-', $member_DATA_JSON->{'dateofbirth'}) ) );
			
			$fcj_Member_Datails_DATA->special_attentions = $member_DATA_JSON->{'specialAttentions'};
			
			
			$fcj_Member_Info = array(
					"member_DATA" => $fcj_Member_DATA,
					"member_Details_DATA" => $fcj_Member_Datails_DATA
					);
			
			// Member may be a reference link
			if ($member_DATA_JSON->{'member_isReferent'} == 'on') {
				
				$fcj_TripToEuroDisney_2013_DATA = ns_FCJ_DB\FCJ_DB_TripToEuroDisney_2013_FACTORY::get_NEW__FCJ_DB_TripToEuroDisney_2013_DATA();
				$fcj_TripToEuroDisney_2013_DATA->member_is_Group_Link = 1;
				$fcj_TripToEuroDisney_2013_DATA->member_Detail_Phone = $member_DATA_JSON->{'member_Ref_Phone'};
				
				$fcj_Member_Info['tripToEuroDisney_2013_DATA'] = $fcj_TripToEuroDisney_2013_DATA;
			}
			
			
			array_push($array_Members_Info, $fcj_Member_Info);
			
		}
		// ------------------------------------------------------------------------------------------------------------------------------------|/ Check Members data \|---		
		
		
		// Check group name -------------------------------------------------------------------------------------------------------------------|\/|---
		$conditions = ""
				."`$fcj_Groups_TABLE->tableName`.`$fcj_Groups_TABLE->field_name` = '$fcj_DB_EuroDisney_Form__DATA->group_name'"
				."";
		
		try {
			$array_Groups =	ns_FCJ_DB\FCJ_DB_Groups_FACTORY::get__Groups_by_Conditions($conditions, $connection);
			if (sizeof($array_Groups) > 0) {
				throw new \Exception("Found duplicated group name.");
			}
			
		} catch (\Exception $e) {
			throw new \Exception("Error in group name. ". $e->getMessage(), 
					ns_FCJ_DB\FCJ_DB_EuroDisney_Form_CONSTANTS::SUBMIT_RESULT__ERROR__NAME_DUPLICATED);
		}
		// ------------------------------------------------------------------------------------------------------------------------------------|/\|---
		
		
		// Check user link ---------------------------------------------------------------------------------------|\/|---
		$fcj_DB_Users_EDITOR = ns_FCJ_DB\FCJ_DB_Users_FACTORY::get_NEW__FCJ_DB_Users_EDITOR();
		$fcj_DB_User_DATA = ns_FCJ_DB\FCJ_DB_Users_FACTORY::get_NEW__FCJ_DB_User_DATA();
		
		$fcj_DB_User_DATA->id = $fcj_DB_EuroDisney_Form__DATA->group_user_Link_ID;
		
		try {
			$fcj_DB_Users_EDITOR->connect_To_DB($connection);
			$fcj_DB_Users_EDITOR->CRUD__Read($fcj_DB_User_DATA);
		} catch (\Exception $e) {
			throw new \Exception("Error in user link. ". $e->getMessage(), 
					ns_FCJ_DB\FCJ_DB_EuroDisney_Form_CONSTANTS::SUBMIT_RESULT__ERROR__USER_LINK);
		}
		// -------------------------------------------------------------------------------------------------------|/\|---
		
		
		// Create Group data -------------------------------------------------------------------------|\/|---
		$fcj_Group_DATA = ns_FCJ_DB\FCJ_DB_Groups_FACTORY::get_NEW__FCJ_DB_Group_DATA();
		
		$fcj_Group_DATA->group_type = ns_FCJ_DB\FCJ_DB_Groups_CONSTANTS::GROUP_TYPE__Standard;
		
		$fcj_Group_DATA->name = $fcj_DB_EuroDisney_Form__DATA->group_name;
		$fcj_Group_DATA->user_link_ID = $fcj_DB_User_DATA->id;
		$fcj_Group_DATA->email = $fcj_DB_EuroDisney_Form__DATA->group_email;
		$fcj_Group_DATA->phone = $fcj_DB_EuroDisney_Form__DATA->group_phone;
		
		try {
			$fcj_Groups_EDITOR->connect_To_DB($connection);
			if (!$fcj_Groups_EDITOR->CRUD__Create($fcj_Group_DATA)) {
				throw new \Exception("Cannot create data.");
			}
		} catch (\Exception $e) {
			throw new \Exception("Error creating new group data. ". $e->getMessage(),
					ns_FCJ_DB\FCJ_DB_EuroDisney_Form_CONSTANTS::SUBMIT_RESULT__ERROR__GROUP_DATA);
		}
		// -------------------------------------------------------------------------------------------|/\|---
		
		
		// Update EuroDisney Form related group ID -------------------------------------------------------------------------------------|\/|---
		$fcj_DB_EuroDisney_Form__EDITOR = ns_FCJ_DB\FCJ_DB_EuroDisney_Form_FACTORY::get_NEW__FCJ_DB_EuroDisney_Form_EDITOR();
		$fcj_DB_EuroDisney_Form__DATA_Update = ns_FCJ_DB\FCJ_DB_EuroDisney_Form_FACTORY::get_NEW__FCJ_DB_EuroDisney_Form_DATA();
		
		$fcj_DB_EuroDisney_Form__DATA_Update->id = $fcj_DB_EuroDisney_Form__DATA->id;
		$fcj_DB_EuroDisney_Form__DATA_Update->related_ID = $fcj_Group_DATA->id;
		
		try {
			$fcj_DB_EuroDisney_Form__EDITOR->connect_To_DB($connection);
			if (!$fcj_DB_EuroDisney_Form__EDITOR->CRUD__Update($fcj_DB_EuroDisney_Form__DATA_Update)) {
				throw new \Exception("Cannot update data.");
			}
		} catch (\Exception $e) {
			throw new \Exception("Error updating EuroDisney Form data. "
						. " [Form_DATA] = ". print_r($fcj_DB_EuroDisney_Form__DATA_Update, true) ." . "
						. $e->getMessage(),
					ns_FCJ_DB\FCJ_DB_EuroDisney_Form_CONSTANTS::SUBMIT_RESULT__ERROR__OTHER);
		}
		
		// -----------------------------------------------------------------------------------------------------------------------------|/\|---
		
		
		// Create TripToEuroDisney 2013 data for gorup --------------------------------------------------------------------------------------------------------|\/|---
		$fcj_TripToEuroDisney_2013_DATA = ns_FCJ_DB\FCJ_DB_TripToEuroDisney_2013_FACTORY::get_NEW__FCJ_DB_TripToEuroDisney_2013_DATA();
		$fcj_TripToEuroDisney_2013_EDITOR = ns_FCJ_DB\FCJ_DB_TripToEuroDisney_2013_FACTORY::get_NEW__FCJ_DB_TripToEuroDisney_2013_EDITOR();
		
		$fcj_TripToEuroDisney_2013_DATA->group_id = $fcj_Group_DATA->id;
		$fcj_TripToEuroDisney_2013_DATA->payment_method = $fcj_DB_EuroDisney_Form__DATA->group_Payment_Method;
		$fcj_TripToEuroDisney_2013_DATA->transport_method = $fcj_DB_EuroDisney_Form__DATA->group_Transport_Method;
		
		
		if ($fcj_DB_EuroDisney_Form__DATA->group_Transport_Method == ns_FCJ_DB\FCJ_DB_TripToEuroDisney_2013_CONSTANTS::TRANSPORT_TYPE__OTHER) {
			
			$extra_DATA = json_decode($fcj_DB_EuroDisney_Form__DATA->extra_DATA);
			$fcj_TripToEuroDisney_2013_DATA->transport_method_Detail = $extra_DATA[FCJ_EuroDisney_CONSTANTS::EXTRA_DATA__Other_Transport_Detail];
			
		}
		
		
		try {
			$fcj_TripToEuroDisney_2013_EDITOR->connect_To_DB($connection);
			if (!$fcj_TripToEuroDisney_2013_EDITOR->CRUD__Create($fcj_TripToEuroDisney_2013_DATA)) {
				throw new \Exception("Cannot create data.");
			}
		} catch (\Exception $e) {
			throw new \Exception("Error creating TripToEuroDisney 2013 data. ". $e->getMessage(),
					ns_FCJ_DB\FCJ_DB_EuroDisney_Form_CONSTANTS::SUBMIT_RESULT__ERROR__OTHER);
		}
		// ----------------------------------------------------------------------------------------------------------------------------------------------------|/\|---
		
		
		// Create members -----------------------------------------------------------------------------------------------------------------------------|\ Create members /|---
		$fcj_Members_EDITOR = ns_FCJ_DB\FCJ_DB_Members_FACTORY::get_NEW__FCJ_DB_Members_EDITOR();
		$fcj_Member_Datails_EDITOR = ns_FCJ_DB\FCJ_DB_Members_Details_FACTORY::get_NEW__FCJ_DB_Members_Details_EDITOR();
		
		foreach ($array_Members_Info as $fcj_Member_Info) {
			
			/* @var $fcj_Member_DATA FCJ_DB_Member_DATA		 */
			$fcj_Member_DATA = $fcj_Member_Info["member_DATA"];	// Get member data
			
			$fcj_Member_DATA->member_type = ns_FCJ_DB\FCJ_DB_Members_CONSTANTS::MEMBER_TYPE__STANDAR;	// Set member type
			$fcj_Member_DATA->group_ID = $fcj_Group_DATA->id;
			
			// Create new member data --------------------------------------------|\/|---
			try {
				$fcj_Members_EDITOR->connect_To_DB($connection);
				if (!$fcj_Members_EDITOR->CRUD__Create($fcj_Member_DATA)) {
					throw new \Exception("Cannot create data.");
				}	
			} catch (\Exception $e) {
				throw new \Exception("Error creating new member data. ". $e->getMessage(),
						ns_FCJ_DB\FCJ_DB_EuroDisney_Form_CONSTANTS::SUBMIT_RESULT__ERROR__OTHER);
			}
			// -------------------------------------------------------------------|/\|---
			
			/* @var $fcj_Member_Datails_DATA FCJ_DB_Member_Details_DATA		 */			
			$fcj_Member_Datails_DATA = $fcj_Member_Info["member_Details_DATA"];	// Get member details data	
			
			$fcj_Member_Datails_DATA->member_ID = $fcj_Member_DATA->id;
			
			// Create new member detail data --------------------------------------------------|\/|---
			try {
				$fcj_Member_Datails_EDITOR->connect_To_DB($connection);
				if (!$fcj_Member_Datails_EDITOR->CRUD__Create($fcj_Member_Datails_DATA)) {
					throw new \Exception("Cannot create data.");
				}
			} catch (\Exception $e) {
				throw new \Exception("Error creating new member detail data. ". $e->getMessage(),
						ns_FCJ_DB\FCJ_DB_EuroDisney_Form_CONSTANTS::SUBMIT_RESULT__ERROR__OTHER);
			}
			// --------------------------------------------------------------------------------|/\|---
			
			// If exists TripToEuroDisney_2013 DATA then create object in database ----------------------------------------------------------------------|\/|---
			if ($fcj_Member_Info["tripToEuroDisney_2013_DATA"] != null) {
				
				$fcj_TripToEuroDisney_2013_EDITOR = ns_FCJ_DB\FCJ_DB_TripToEuroDisney_2013_FACTORY::get_NEW__FCJ_DB_TripToEuroDisney_2013_EDITOR();
				$fcj_TripToEuroDisney_2013_DATA = $fcj_Member_Info["tripToEuroDisney_2013_DATA"];
				
				$fcj_TripToEuroDisney_2013_DATA->group_id = $fcj_Member_DATA->group_ID;
				$fcj_TripToEuroDisney_2013_DATA->member_id = $fcj_Member_DATA->id;
				
				try {
					$fcj_TripToEuroDisney_2013_EDITOR->connect_To_DB($connection);
					if (!$fcj_TripToEuroDisney_2013_EDITOR->CRUD__Create($fcj_TripToEuroDisney_2013_DATA)) {
						throw new \Exception("Cannot create data.");
					}
				} catch (\Exception $e) {
					throw new \Exception("Error creating TripToEuroDisney 2013 data. ". $e->getMessage(),
							ns_FCJ_DB\FCJ_DB_EuroDisney_Form_CONSTANTS::SUBMIT_RESULT__ERROR__OTHER);
				}
				
			}
			// -----------------------------------------------------------------------------------------------------------------------------------------|/\|---
			
			
		}
		// --------------------------------------------------------------------------------------------------------------------------------------------|/ Create members \|---
		
	}
	
	
	/**
	 * Method for add family data
	 */
	protected function add_Family_DATA() {
		
		
		$fcj_DB_EuroDisney_Form__DATA = ns_FCJ_DB\FCJ_DB_EuroDisney_Form_FACTORY::get_NEW__FCJ_DB_EuroDisney_Form_DATA();
		
		
		// Initilize basic data -------------------------------------------------------|\/|---
		$fcj_DB_EuroDisney_Form__DATA->time_at_insert = date('Y-m-d H:i:s');
		$fcj_DB_EuroDisney_Form__DATA->remote_IP = $_SERVER['REMOTE_ADDR'];
		$fcj_DB_EuroDisney_Form__DATA->remote_browser_DATA = $_POST['browser_DATA'];
		// ------------------------------------------------------------------------------|/\|---

		
		// Get JSON data from received data -----------------------------------|\/|---
// 		$fcj_family_DATA__JSON = json_decode($_POST['family_DATA']);
		$fcj_family_DATA__JSON = ns_php_Util\php_Util::_json_decode($_POST['family_DATA']); // json_decode($_POST['family_DATA']);
		
// 		$fcj_members_DATA__JSON = json_decode($_POST['members_DATA']);
		$fcj_members_DATA__JSON = ns_php_Util\php_Util::_json_decode($_POST['members_DATA']); // json_decode($_POST['family_DATA']);
// 		$fcj_members_DATA__JSON = json_decode($_POST['members_DATA'], true, 256, JSON_BIGINT_AS_STRING);
		
// 		$services_JSON = new \Services_JSON();
// 		$fcj_family_DATA__JSON = $services_JSON->decode($_POST['family_DATA']);
// 		$fcj_members_DATA__JSON = $services_JSON->decode($_POST['members_DATA']);
		// --------------------------------------------------------------------|/\|---
		
		
		// Set EuroDisney Form data -------------------------------------------------------------------------------|\/|---
		$fcj_DB_EuroDisney_Form__DATA->group_name = $fcj_family_DATA__JSON->{'name'};
		$fcj_DB_EuroDisney_Form__DATA->group_phone = $fcj_family_DATA__JSON->{'phone'};
		$fcj_DB_EuroDisney_Form__DATA->group_user_Link_ID = $fcj_family_DATA__JSON->{'educator'};
		$fcj_DB_EuroDisney_Form__DATA->group_email = $fcj_family_DATA__JSON->{'mail'};
		$fcj_DB_EuroDisney_Form__DATA->group_Payment_Method = $fcj_family_DATA__JSON->{'payment_Method'};
		$fcj_DB_EuroDisney_Form__DATA->group_Transport_Method = $fcj_family_DATA__JSON->{'transport_Method'};
		
		
		$extra_DATA = array(
				FCJ_EuroDisney_CONSTANTS::EXTRA_DATA__Other_Transport_Detail => $fcj_family_DATA__JSON->{FCJ_EuroDisney_CONSTANTS::EXTRA_DATA__Other_Transport_Detail}
		);
		
		$fcj_DB_EuroDisney_Form__DATA->extra_DATA = json_encode($extra_DATA);
		$fcj_DB_EuroDisney_Form__DATA->members_DATA = json_encode($fcj_members_DATA__JSON);
		// --------------------------------------------------------------------------------------------------------|/\|---
		
		
		// Set database connection ---------------------------------------------------------------------|\/|---
// 		$fcj_DB_Connection = ns_php_Util\DB\php_Util_DB_FACTORY::get_NEW__php_Util_DB_Connection();
// 		$fcj_DB_Connection->type = ns_php_Util\DB\php_Util_DB_CONSTANTS::DB_CONNECTION_TYPE__PDO;
// 		$fcj_DB_Connection->hostName = "localhost";
// 		$fcj_DB_Connection->default_DB = "fcj_db";
// 		$fcj_DB_Connection->userName = "fcj_user";
// 		$fcj_DB_Connection->password = "fcj_password";
		
		$fcj_DB_Connection = ns_FCJ_DB\FCJ_DB_FACTORY::get_Connection();
		// ---------------------------------------------------------------------------------------------|/\|---
		
		
		// Initialize database connection -------------------------------------------------------------------------------------|\/|---
		$fcj_DB_EuroDisney_Form__EDITOR = ns_FCJ_DB\FCJ_DB_EuroDisney_Form_FACTORY::get_NEW__FCJ_DB_EuroDisney_Form_EDITOR();
		try {
			$fcj_DB_EuroDisney_Form__EDITOR->connect_To_DB($fcj_DB_Connection);
		} catch (\Exception $e) {
			echo print_r($e);	// TODO REMOVE DEBUG LOG
			throw new \Exception("Error in database connection. ". $e->getMessage());
		}
		// --------------------------------------------------------------------------------------------------------------------|/\|---	
		
		
		// Create object -----------------------------------------------------------------------------|\/|---
		try {
			if (!$fcj_DB_EuroDisney_Form__EDITOR->CRUD__Create($fcj_DB_EuroDisney_Form__DATA)) {
				throw new \Exception("Cannot create form data.");
			}	
		} catch (\Exception $e) {
			echo print_r($e);	// TODO REMOVE DEBUG LOG
			throw new \Exception("Error creating form data. ". $e->getMessage());
		}
		// -------------------------------------------------------------------------------------------|/\|---		
		
		
		
		$message_Process_Form = array(
				"type" => null,
				"message" => "",  
				);
		
		try {
			
			$this->process_EuroDisney_Form_DATA($fcj_DB_EuroDisney_Form__DATA, $fcj_DB_Connection);
			
			$message_Process_Form["type"] = "ok";
			$message_Process_Form["message"] = "Data inserted";
			$message_Process_Form["received_DATA"] = print_r($fcj_DB_EuroDisney_Form__DATA, true);
			
			$fcj_DB_EuroDisney_Form__DATA->submit_Result = 0;
			
			
			
		} catch (\Exception $e) {
			
			$message_Process_Form["type"] = "error";
			$message_Process_Form["error_Code"] = $e->getCode();
			$message_Process_Form["message"] = $e->getMessage();
			$message_Process_Form["received_DATA"] = print_r($fcj_DB_EuroDisney_Form__DATA, true);
			
			
			
			$fcj_DB_EuroDisney_Form__UPDATE__DATA = ns_FCJ_DB\FCJ_DB_EuroDisney_Form_FACTORY::get_NEW__FCJ_DB_EuroDisney_Form_DATA();
			
			$fcj_DB_EuroDisney_Form__UPDATE__DATA->id = $fcj_DB_EuroDisney_Form__DATA->id;
			$fcj_DB_EuroDisney_Form__UPDATE__DATA->submit_Result = $e->getCode();
			
			ns_php_Util\php_Util::array_push_associative( $extra_DATA, array( "error_Message" => $e->getMessage() ) );
			$fcj_DB_EuroDisney_Form__UPDATE__DATA->extra_DATA = $extra_DATA;
			
			// Update form data --------------------------------------------------------------------------|\/|---
			try {
				if (!$fcj_DB_EuroDisney_Form__EDITOR->CRUD__Update($fcj_DB_EuroDisney_Form__UPDATE__DATA)) {
					throw new \Exception("Cannot update form data.");
				}
			} catch (\Exception $e) {
				echo print_r($e);	// TODO REMOVE DEBUG LOG
				throw new \Exception("Error updating form data. ". $e->getMessage());
			}
			// -------------------------------------------------------------------------------------------|/\|---
			
			
			
// 			$fcj_DB_EuroDisney_Form__DATA->submit_Result
		}
		
		
		return json_encode( $message_Process_Form );
// 		echo print_r("Data inserted");	// TODO REMOVE DEBUG LOG
// 		echo print_r($fcj_DB_EuroDisney_Form__DATA);	// TODO REMOVE DEBUG LOG
	}
	
}



// Main -----------------------------------------------|\/|---
// error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
error_reporting(E_ERROR);

header('Content-Type: text/html; charset=UTF-8');

$form_Add_Family = new FCJ_EuroDisney();
try {
	$response = $form_Add_Family->manage_AJAX();
	echo $response;
} catch (\Exception $e) {
	echo print_r($e->getMessage());	// TODO REMOVE DEBUG LOG
}
// ----------------------------------------------------|/\|---


